package com.ztgf.order.service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.common.bo.HeadParamsBO;
import com.ztgf.common.constant.CommonParameter;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.db.utils.RedisUtil;
import com.ztgf.dubbo.exception.ServiceException;
import com.ztgf.order.common.config.YmlConfig;
import com.ztgf.order.mapper.OrderPaymentScaleMapper;
import com.ztgf.order.models.bo.OrderPaymentScaleBO;
import com.ztgf.order.models.dto.OrderInfoDTO;
import com.ztgf.order.models.dto.OrderPaymentScaleDTO;
import com.ztgf.order.models.service.IOrderPayRelationService;
import com.ztgf.order.models.service.IOrderPaymentScaleService;
import com.ztgf.order.models.vo.OrderPaymentScaleVO;
import com.ztgf.user.models.dto.UserInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 订单表控制器
 *
 * @author chenyu.wang
 * @Date 2020-06-03 15:32:26
 */
@Slf4j
@DubboService
@Lazy
public class OrderPaymentScaleServiceImpl extends ServiceImpl<OrderPaymentScaleMapper, OrderPaymentScaleDTO> implements IOrderPaymentScaleService {

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private IOrderPayRelationService orderPayRelationService;

    @Override
    public OrderPaymentScaleVO saveOrderPaymentScale(OrderPaymentScaleBO orderPaymentScaleBO){

        return null;
    }

    @Override
    public Result addPaymentScaleByList(List<OrderPaymentScaleBO> orderPaymentScaleBOList, Long paymentId, OrderInfoDTO orderInfoDTO) {
        log.info("设置赊销比例开始---------------start");
        HeadParamsBO headParamsBO = CommonParameter.getHeadParamsBO();
        UserInfoDTO userInfoDTO = (UserInfoDTO) redisUtil.getSession(headParamsBO.getSessionId(), headParamsBO.getSource() + "");//获取操作人
        BigDecimal orderPaymentScale = new BigDecimal(1.00);
        BigDecimal actualMoneyScale = new BigDecimal(0.00);
        BigDecimal actualMoneyTotal = new BigDecimal(0.00);
        for(OrderPaymentScaleBO orderPaymentScaleBO : orderPaymentScaleBOList){
            //要求支付比例总和等于1
            actualMoneyScale = actualMoneyScale.add(orderPaymentScaleBO.getPaymentScale());
        }
        log.info("赊销比例:actualMoneyScale---------------"+actualMoneyScale);
        if(actualMoneyScale.compareTo(orderPaymentScale) == 0){
            for(int i =0 ; i<orderPaymentScaleBOList.size(); i++){
                OrderPaymentScaleBO orderPaymentScaleBO = orderPaymentScaleBOList.get(i);
                OrderPaymentScaleDTO orderPaymentScaleDTO = new OrderPaymentScaleDTO();
                BigDecimal actualMoney = (orderPaymentScaleBO.getPaymentScale().multiply(orderInfoDTO.getActualMoney())).setScale(2, BigDecimal.ROUND_HALF_UP);
                BeanUtils.copyProperties(orderPaymentScaleBO, orderPaymentScaleDTO);
                orderPaymentScaleDTO.setPaymentId(paymentId);
                if(i != orderPaymentScaleBOList.size() - 1 ){
                    //根据比例计算总金额
                    actualMoneyTotal = actualMoneyTotal.add(actualMoney);
                    orderPaymentScaleDTO.setContractAmount(actualMoney);//合同金额
                    orderPaymentScaleDTO.setPaymentAmount(actualMoney);//实付金额
                }else{
                    //保证精度
                    orderPaymentScaleDTO.setContractAmount(orderInfoDTO.getActualMoney().subtract(actualMoneyTotal));//合同金额
                    orderPaymentScaleDTO.setPaymentAmount(orderInfoDTO.getActualMoney().subtract(actualMoneyTotal));//实付金额
                }

                orderPaymentScaleDTO.setId(ymlConfig.getPkId());
                orderPaymentScaleDTO.setPeriods(i+1);
                //保存总金额
                orderPaymentScaleDTO.setPaymentTotal(orderInfoDTO.getActualMoney());
                //orderPaymentScaleDTO.setCreateUserId(userInfoDTO.getCreateUserId());
                //orderPaymentScaleDTO.setUpdateUserId(userInfoDTO.getUpdateUserId());
                this.insert(orderPaymentScaleDTO);

            }
        }else{
            log.info("设置赊销比例有误---------------start");
            throw new ServiceException(CodeEnum.FAIL_100076);
        }

        return new Result(CodeEnum.SUCCESS);
    }

}
